package services

import (
	"fmt"
	"strings"
)

type GitPushType int32

const (
	PUSH_COMMIT GitPushType = iota
	ADD_BRANCH
	DELETE_BRANCH
	PUSH_TAG
	UNKONWN
)

var (
	PushTitle = map[GitPushType]string{
		PUSH_COMMIT:   "代码提交提醒",
		ADD_BRANCH:    "新增分支提醒",
		DELETE_BRANCH: "删除分支提醒",
		PUSH_TAG:      "标签提交提醒",
		UNKONWN:       "默认提醒",
	}
)

func GitLabWebHook(from, key string, gitlabmsg map[string]any) int {
	switch from {
	case "feishu":
		pushType, content := parser_gitlab_msg(from, gitlabmsg)
		return push_feishu_markdown(key, pushType, content)
	case "weixin":
		pushType, content := parser_gitlab_msg(from, gitlabmsg)
		return push_weixin_markdown(key, pushType, content)
	default:
		return 0
	}
}

func parser_gitlab_msg(from string, gitlabmsg map[string]any) (GitPushType, string) {
	var content string = ""
	var pushType GitPushType = UNKONWN
	object_kind, ok := gitlabmsg["object_kind"].(string)
	if !ok {
		return pushType, ""
	}
	var repository = ""
	repository_map, ok := gitlabmsg["repository"].(map[string]any)
	if ok {
		homepage := repository_map["homepage"].(string)
		repository = fmt.Sprintf("[%v](%s)", repository_map["name"], homepage)
	}
	switch object_kind {
	case "push":
		ref := gitlabmsg["ref"].(string)
		branch := strings.TrimPrefix(ref, "refs/heads/")
		total_commit_count := int(gitlabmsg["total_commits_count"].(float64))
		if total_commit_count > 0 {
			content += fmt.Sprintf("[%s](%s)推送了%d个新的提交到%s的[%s](%s)分支\n", gitlabmsg["user_name"], "", total_commit_count, repository, branch, "")
			commits := gitlabmsg["commits"].([]any)
			for _, commit := range commits {
				commit := commit.(map[string]any)
				if from == "feishu" {
					content += fmt.Sprintf("💡 [%s](%s) - %s \n", commit["id"].(string)[0:8], commit["url"], commit["message"])
				} else {
					content += fmt.Sprintf("> 💡 [%s](%s) - %s \n", commit["id"].(string)[0:8], commit["url"], commit["message"])
				}
			}
			pushType = PUSH_COMMIT
		} else if total_commit_count == 0 {
			if gitlabmsg["before"].(string)[0:8] == "00000000" &&
				gitlabmsg["after"].(string)[0:8] != "00000000" {
				content += fmt.Sprintf("[%s](%s)推送了分支\n 仓库: %s\n 分支: [%s](%s)\n", gitlabmsg["user_name"], "", repository, branch, "")
				pushType = ADD_BRANCH
			} else if gitlabmsg["before"].(string) != "00000000" &&
				gitlabmsg["after"].(string)[0:8] == "00000000" {
				content += fmt.Sprintf("[%s](%s)删除了分支\n 仓库: %s\n 分支: ~~[%s](%s)~~\n", gitlabmsg["user_name"], "", repository, branch, "")
				pushType = DELETE_BRANCH
			} else {
				content += fmt.Sprintf("[%s](%s)操作了%s的[%s](%s)分支\n", gitlabmsg["user_name"], "", repository, branch, "")
			}

		} else {
			content += "push error total count\n"
		}

	case "tag_push":
		ref := gitlabmsg["ref"].(string)
		tag := strings.TrimPrefix(ref, "refs/tags/")
		content += fmt.Sprintf("[%s](%s)推送了新的Tag(%s)到%s\n", gitlabmsg["user_name"], "", tag, repository)
		pushType = PUSH_TAG
	default:
		content += fmt.Sprintf("仓库%s收到新通知 %s\n", repository, object_kind)
	}
	return pushType, content
}
